using System;
using System.Net;
using Communication;

namespace Storage.Server
{
	internal class StorageServer : IDisposable
	{
		public StorageServer(IStorage storage, IPEndPoint endPoint)
		{
			handler = new StorageServerHandler(storage);
			server = new TcpServer(handler, endPoint);
		}

		public StorageServer(IStorage storage, IPAddress ip, int port)
			: this (storage, new IPEndPoint(ip, port)) {}

		public StorageServer(IStorage storage, int port)
			: this(storage, new IPEndPoint(IPAddress.Any, port)) { }

		public void Start()
		{
			server.StartAsync();
		}

		public void Stop()
		{
			server.Stop();
		}

		public void Dispose()
		{
			server.Dispose();
			handler.Dispose();
		}

		private readonly StorageServerHandler handler;
		private readonly IServer server;
	}
}